package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BusinessException;
import com.itheima.reggie.dao.DishMapper;
import com.itheima.reggie.dao.SetmealMapper;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    /*
    * 新增套餐
    * */
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        Long id = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item)->{
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /*
    * 删除套餐
    * */
    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        //1、判断当前要删除的套餐是否处于售卖状态，如果是，不允许删除
        LambdaQueryWrapper<Setmeal> qw1 = new LambdaQueryWrapper<>();
        // select count(*) from setmeal where id in (1,2,3) and status = 1
        qw1.eq(Setmeal::getStatus,1);
        qw1.in(Setmeal::getId,ids);
        //套餐菜品中的状态数量,去判断套餐是否须要删除
        int count = this.count(qw1);
        if (count>0){
            //不可以被删除,抛异常
            throw new BusinessException("该套餐正在售卖,不要删除");
        }
        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);
        //2、删除套餐对应的菜品数据
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        //delete from setmeal_dish where setmeal_id in (1,2,3)
        qw.in(SetmealDish::getDishId,ids);
        setmealDishService.remove(qw);
    }

    /*
    * 根据id查询套餐信息
    * */
    @Override
    @Transactional
    public SetmealDto getByIdWithDish(Long id) {
        //1、根据套餐id查询套餐数据 select * from setmeal where id =?
        Setmeal setmeal = this.getById(id);//获取套餐信息
        //2、根据套餐id查询套餐对应的菜品数据 select * from setmeal_dish where setmeal_id = ?
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> dishlist = setmealDishService.list(qw);

        //3、将套餐 setmeal 和套餐菜品集合 setmealDishList 数据封装到 SetmealDto并返回
        // 3.0 首先new一个SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        // 3.1、BeanUtils.copyProperties() 拷贝setmeal到setmealDto中
        BeanUtils.copyProperties(setmeal,setmealDto);
        // 3.2、将setmealDishList 设置到 setmealDto中
        setmealDto.setSetmealDishes(dishlist);
        //4、返回 setmealDto
        return setmealDto;
    }

    /*
    * 修改套餐
    * */
    @Override
    @Transactional
    public void updateSetmeal(SetmealDto setmealDto) {
        //1、根据id更新 setmeal 套餐表数据 update setmeal set xx=?,.. where id =?
        this.updateById(setmealDto);

        //2、操作套餐对应的菜品数据： setmeal_dish
        //2.1 先根据套餐id删除套餐对应的菜品数据： delete from setmeal_dish where setmeal_id =?
        // 获取套餐id   setmealDto.getId()
        Long id = setmealDto.getId();
        // 使用 setmealDishMapper 根据套餐id进行删除 ：delete from setmeal_dish where setmeal_id =?
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getId,id);
        setmealDishService.remove(qw);
        //2.2 重新添加新的套餐对应的菜品数据 insert setmeal_dish values (..)
        // setmealDto.getSetmealDishes() 获取套餐的菜品集合 List<SetmealDish>
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        // 遍历套餐菜品集合数据
       // for (SetmealDish setmealDish : setmealDishes) {
            // 有没有数据需要set ： 设置套餐id  setSetmealId
        for (SetmealDish setmealDish : setmealDishes) {
            //获取套餐分类
            Long setmealId = setmealDish.getSetmealId();
            Category category = categoryService.getById(setmealId);
            //获取到了当前分类的名称
            String categoryName = category.getName();
            //新建setmealDto将分类名称set进入
            SetmealDto setmealDtoPlus = new SetmealDto();
            setmealDtoPlus.setCategoryName(categoryName);
            BeanUtils.copyProperties(setmealDish,setmealDtoPlus);
            // 添加套餐菜品数据 insert

        }

    }
}
